<!DOCTYPE html>
<html lang="zh-CN"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="model/boardModel">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script th:src="@{/plugins/My97DatePicker/WdatePicker.js}"></script>
    <title>待收货列表</title>

</head>
<body>

<div th:fragment="pagenav" th:remove="tag">
    <div th:replace="model/nav :: pageheader ('收货管理','收货管理')"></div>
</div>

<div th:fragment="content" th:remove="tag">


    <div class="row">
        <div class="col-xs-12">

            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">查询条件</h3>
                    <div class="box-tools pull-right">
                        <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                    </div>
                </div>
                <!-- /.box-header -->
                <!-- form start -->
                <form role="form" id="queryForm" class="form-horizontal" th:action="@{/express/list}" method="post" >
                    <div class="box-body">
                        <div class="row">
                                <div class="col-xs-3">
                                    <input type="text" th:name="expressNumber"  class="form-control input-sm" placeholder="发货单号"/>
                                </div>
                            <div class="col-xs-3">
                                <input type="text" th:name="trackNumber" class="form-control input-sm" placeholder="快递单号"/>
                            </div>
                            <div class="col-xs-1" style="font-size: 16px;line-height: 32px;">
                                状态：
                            </div>
                            <div class="col-xs-3">
                                <select name="expressStatus" style="width: 160px;height: 34px;float:left" class="form-control select2" >
                                    <option value="">全部</option>
                                    <option value="400">待收货</option>
                                    <option value="500">已收货</option>
                                </select>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-1" >
                                开始时间：
                            </div>
                            <div class="col-xs-2">
                                <input type="text" th:name="startTime" class="form-control input-sm" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd ',maxDate:'%y-%M-%d',readOnly:true});resetWdatePickerPosition(this);" placeholder="开始时间"/>
                            </div>
                            <div class="col-xs-1" >
                                截止时间：
                            </div>
                            <div class="col-xs-2">
                                <input type="text" th:name="endTime" class="form-control input-sm" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd ',maxDate:'%y-%M-%d',readOnly:true});resetWdatePickerPosition(this);" placeholder="截止时间"/>
                            </div>
                            <div class="col-xs-3">
                                <button id="btn-query" type="button"  class="btn btn-primary  btn-sm">查询</button>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->

                    <div class="box-footer">

                    </div>
                </form>
            </div>
            <form role="form" id="submitForm" class="form-horizontal"  method="post" >
                <div class="box-body" id="orderforSubmit">

                </div>
            </form>

            <div class="box">

                <!-- /.box-header -->
                <div class="box-body">
                    <div class="table-responsive">
                    <table id="dataTable" class="table table-striped table-bordered table-hover table-condensed">

                        <thead >
                        <tr>
                            <th>发货单号</th>
                            <th>物流公司</th>
                            <th>物流单号</th>
                            <th>手机数量</th>
                            <th>所属门店</th>
                            <th>发货时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                    </table>
                    </div>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
    </div>

    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" style="background-color: #307095;">
                    <button type="button" class="close" data-dismiss="modal" id="btn_close" >
                        <span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel2">订单列表</h4>
                </div>

                <div class="modal-body">
                    <div class="box-body">
                        <table id="goodsTable" class="table table-striped table-bordered table-hover table-condensed">

                            <thead id="theadTable">
                                <tr>
                                    <th>订单编号</th>
                                    <th>物品名称</th>
                                    <th>旧机款</th>
                                    <th >IMEI</th>
                                    <th>状态</th>
                                </tr>
                            </thead>

                        </table>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" id="btn-submit" class="btn btn-primary">确定</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="verifyModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" style="background-color: #307095;">
                    <button type="button" class="close" onclick="clearAllData();" data-dismiss="modal"  >
                        <span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel2">订单列表</h4>
                </div>

                <div class="modal-body">
                    <div class="box-body">
                        <table id="verifyTable" class="table table-striped table-bordered table-hover table-condensed">

                            <thead id="theadTable2">
                            <tr>
                                <!--<th><input type="checkBox" id="selectAll" onclick="selectAll()" value="全选"/></th>-->
                                <th>订单编号</th>
                                <th>物品名称</th>
                                <th>价格</th>
                                <th align='center'>IMEI</th>
                                <th>状态</th>
                                <th align='center'>操作</th>
                            </tr>
                            </thead>

                        </table>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" id="btn-verify" onclick="verify();" class="btn btn-primary">确认</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="printModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" style="background-color: #307095;">
                    <button type="button" class="close" onclick="clearAllData();" data-dismiss="modal"  >
                        <span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel3">订单列表</h4>
                </div>

                <div class="modal-body">
                    <div class="box-body">
                        <table id="printTable" class="table table-striped table-bordered table-hover table-condensed">

                            <thead id="printTable2">
                            <tr>
                                <th><input type="checkBox" id="selectAll" onclick="selectAll()" value="全选"/></th>
                                <th>订单编号</th>
                                <th>物品名称</th>
                                <th>价格</th>
                                <th align='center'>IMEI</th>
                                <th>状态</th>
                                <th align='center'>操作</th>
                            </tr>
                            </thead>

                        </table>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" id="btn-print" onclick="print();" class="btn btn-primary">确认</button>
                </div>
            </div>
        </div>
    </div>

    <!-- DataTables -->
    <script type="text/javascript">

        $(function (){
            var tables = $("#dataTable").dataTable({
                serverSide: true,//分页，取数据等等的都放到服务端去
                processing: true,//载入数据的时候是否显示“载入中”
                pageLength: 10,  //首次加载的数据条数
                ordering: false,//排序操作在服务端进行，所以可以关了。
                /*pagingType: "full_numbers",*/
                autoWidth: false,
                stateSave: true,//保持翻页状态，和tables.fnDraw(false);结合使用
                searching: false,
                ajax: {   //类似jquery的ajax参数，基本都可以用。
                    type: "post",//后台指定了方式，默认get，外加datatable默认构造的参数很长，有可能超过get的最大长度。
                    url: "/express/list",
                    dataSrc: "data",//默认data，也可以写其他的，格式化table的时候取里面的数据
                    data: function (d) {//d是原始的发送给服务器的数据，默认很长。
                        var param = {}; //因为服务端排序，可以新建一个参数对象
                        param.draw = d.draw;
                        param.start = d.start;
                        param.length = d.length;
                        var formData = $("#queryForm").serializeArray();//把form里面的数据序列化成数组
                        formData.forEach(function (e) {
                            param[e.name] = e.value;
                        });
                        return param;//自定义需要传递的参数。
                    }
                },
                columns: [//对应上面thead里面的序列
                    {"data": 'expressNumber'},
                    {"data": 'company',defaultContent: ""},
                    {"data": 'trackNumber'}, //mData 表示发请求时候本列的列明，返回的数据中相同下标名字的数据会填充到这一列
                    {"data": 'orderSum'},
                    {"data": 'optStore',defaultContent: ""},
                    {"data": 'createTimeStr', defaultContent: ""},
                    {"data": 'expressStatus', defaultContent: "",
                        "render": function (data, type, full, callback) {
                            if(data==400)
                                return  /*<![CDATA[*/"<span class='label label-success'>待收货</span>"/*]]>*/;
                            else if(data==500)
                                return /*<![CDATA[*/"<span class='label label-danger'>已收货</span>"/*]]>*/;
                        }
                    },
                    {
                        "data": "expressStatus","width": "240px",
                        "render": function (data, type, full, callback) {
                            /*<![CDATA[*/
                            if (data == 400)
                                return "<div class='btn-group'><button id='editRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-edit'></i>查看详情</button>" +
                                    "<button id='delRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-hand-pointer-o'></i>确认收货</button></div>"+
                                    "<button id='printCode' class='btn btn-primary btn-sm' type='button'><i class='fa fa-hand-pointer-o'></i>打印条码</button></div>";
                            else if (data == 500)
                                return "<div class='btn-group'><button id='editRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-edit'></i>查看详情</button></div>";

                            /*]]>*/
                        }
                    }

                ],

                /*<![CDATA[*/
              /*  columnDefs: [
                    {
                        targets: -1,//编辑
                        data: null,//下面这行，添加了编辑按钮和，删除按钮
                        defaultContent: "<div class='btn-group'><button id='editRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-edit'></i>查看详情</button>" +
                        "<button id='delRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-hand-pointer-o'></i>确认收货</button></div>"
                    },
                    {
                        targets: -1,//编辑
                        data: "expressStatus==WAITING_FOR_CHECK",//下面这行，添加了编辑按钮和，删除按钮
                        defaultContent: "<div class='btn-group'><button id='editRow' class='btn btn-primary btn-sm' type='button'><i class='fa fa-edit'></i>查看详情</button></div>"
                    }
                ],*/
                dom: '<"toolbar">frtip',
                /*]]>*/

                //每加载完一行的回调函数
                createdRow: function( row, data, index ) {
                    //修改单元格样式
                    //$('td', row).eq(5).css('font-weight',"bold").css("color","red");//获取到具体行具体格的元素
                },
                initComplete: function (setting, json) {
                    //初始化完成之后替换原先的搜索框。
                },
                //在每次table被draw完后调用
                /*fnDrawCallback: function(){
                 var api = this.api();
                 //获取到本页开始的条数
                 var startIndex= api.context[0]._iDisplayStart;
                 api.column(0).nodes().each(function(cell, i) {
                 cell.innerHTML = startIndex + i + 1;
                 });
                 },*/


                language: {
                    lengthMenu: "",//不显示记录条数选择
                    //lengthMenu: '<select class="form-control input-xsmall">' + '<option value="5">5</option>' + '<option value="10">10</option>' + '<option value="20">20</option>' + '<option value="30">30</option>' + '<option value="40">40</option>' + '<option value="50">50</option>' + '</select>条记录',//左上角的分页大小显示。
                    processing: "加载中",//处理页面数据的时候的显示
                    /*<![CDATA[*/
                    paginate: {//分页的样式文本内容。
                        previous: "<",
                        next: ">",
                        first: "<<",
                        last: ">>"
                    },
                    /*]]>*/
                    zeroRecords: "没有数据",//table tbody内容为空时，tbody的内容。
                    //下面三者构成了总体的左下角的内容。
                    //info: "共 _PAGES_ 页，显示第 _START_ 到第 _END_ 条 ",//筛选之后得到 _TOTAL_ 条，初始 _MAX_ 条   左下角的信息显示，大写的词为关键字。
                    info: "共 _PAGES_ 页，显示第 _START_ 到第 _END_ 条",
                    infoEmpty: "0条记录",//筛选为空时左下角的显示。0条记录
                    infoFiltered: "",//筛选之后的左下角筛选提示(另一个是分页信息显示，在上面的info中已经设置，所以可以不显示)，
                    sSearch: "关键字："
                }

            });


            //查询按钮
            $("#btn-query").on("click", function () {
                tables.fnDraw();
            });
            //打印条码
            $("#dataTable tbody").on("click", "#printCode", function () {
                    var data = tables.api().row($(this).parents("tr")).data();
                    $.ajax({
                        url: '/express/getGoods',
                        type: 'POST',
                        data: {"expressId": data.expressId},
                        dataType: "json",
                        cache: "false",
                        success: function (result) {
                        if (result.result == 200) {
                            $.each(result.data,function(i,n){
                                /*<![CDATA[*/
                                $("#printTable2").append("<tr><td><input type='checkbox' id='"+n.goodsId+"'/></td><td>"+n.orderNo+"</td><td>"+n.goodsName+"</td>" +
                                    "<td>"+n.price+"</td><td>"+n.imei+"</td><td>"+n.orderMidStatus+"</td>" +
                                    "<td><div id='bcTarget' class='barcodeImg'></div><button class='btn btn-success' style='width: 46px;height: 21px;font-size: 12px;text-align: center;padding-top: 2px;' onclick='printThis(\""+n.goodsId+"\",this);'>打印</button>" +
                                    "</td></tr>");
                                /*]]>*/
                            });

                        } else {
                            toastr.error(result.msg);
                        }
                    },
                    error: function (err) {
                        toastr.error("网络错误...");
                    }
                });
                    $("#printModal").modal("show");
                });

            //查看详情
           $("#dataTable tbody").on("click", "#editRow", function () {
               var data = tables.api().row($(this).parents("tr")).data();

               $.ajax({
                   url: '/express/getGoods',
                   type: 'POST',
                   data: {"expressId": data.expressId},
                   dataType: "json",
                   cache: "false",
                   success: function (result) {
                       if (result.result == 200) {
                           $.each(result.data,function(i,n){
                               /*<![CDATA[*/
                               $("#theadTable").append("<tr><td>"+n.orderNo+"</td><td>"+n.goodsName+"</td><td>"+n.price+"</td><td>"+n.imei+"</td><td>"+n.orderMidStatus+"</td></tr>");
                               /*]]>*/
                           });

                       } else {
                           toastr.error(result.msg);
                       }
                   },
                   error: function (err) {
                       toastr.error("网络错误...");
                   }
               });
                $("#editModal").modal("show");
            });
            $('#editModal').on('hide.bs.modal', function () {
                $("#goodsTable tr:gt(0)").remove();
                $("#editModal").css('display','none');
                $("#loginName").removeAttr("readonly");
            })

            $('#verifyModal').on('hide.bs.modal', function () {
                clearAllData();
            });
            $('#printModal').on('hide.bs.modal', function () {
                $("#printTable2 tr:gt(0)").remove();
            });

           /* $("#btn_close").click(function () {
                $("#goodsTable tr:gt(0)").remove();
                $("#editModal").css('display','none');
                $("#loginName").removeAttr("readonly");
            });*/
            //添加、修改  表单异步提交
            $("#btn-submit").on("click", function(){
                $("#goodsTable tr:gt(0)").remove();
                $("#editModal").modal("hide");
            });

            //确认收货
            $("#dataTable tbody").on("click", "#delRow", function () {
                var data = tables.api().row($(this).parents("tr")).data();
                $.ajax({
                    url: '/express/getGoods',
                    type: 'POST',
                    data: {"expressId": data.expressId},
                    dataType: "json",
                    cache: "false",
                    success: function (result) {
                        if (result.result == 200) {
                            $.each(result.data,function(i,n){
                                /*<![CDATA[*/
                                $("#theadTable2").append("<tr><td>"+n.orderNo+"</td><td>"+n.goodsName+"</td><td>"+n.price+"</td><td>"+n.imei+"</td><td>"+n.orderMidStatus+"</td><td><button class='btn btn-success' style='width: 46px;height: 21px;font-size: 12px;text-align: center;padding-top: 2px;' onclick='ensure(\""+data.expressId+"\",\""+n.orderId+"\",this);'>确认</button><button class='btn btn-primary' style='width: 54px;height: 21px;font-size: 12px;text-align: center;padding-top: 2px;' onclick='disReceive(\""+data.expressId+"\",\""+n.orderId+"\",this);'>未收到</button></td></tr>");
                                /*]]>*/
                            });

                        } else {
                            toastr.error(result.msg);
                        }
                    },
                    error: function (err) {
                        toastr.error("网络错误...");
                    }
                });
                $("#verifyModal").modal("show");
            });
            $("#btn-verify").click(function () {
                if($("#theadTable2").find("button").length!=0){
                    return toastr.error("请操作每一条订单记录！");
                }else {
                    $("#btn-verify").attr("disabled",true);
                    $.ajax({
                        url: '/express/confirmAll',
                        type: 'POST',
                        data: $("#submitForm").serialize(),
                        dataType: "json",
                        cache: "false",
                        success: function (result) {
                            if (result.result == 200) {
                                $("#btn-verify").attr("disabled",false);
                                $("#verifyModal").modal("hide");
                                toastr.success(result.msg);
                                $("#orderforSubmit > input").remove();
                                $("#theadTable2 tr:gt(0)").remove();
                                i=0;
                                tables.fnDraw();
                                //window.location.reload();
                            } else {
                                $("#btn-verify").attr("disabled",false);
                                toastr.error(result.msg);
                            }
                        },
                        error: function (err) {
                            $("#btn-verify").attr("disabled",false);
                            toastr.error("网络错误...");
                        }
                    });
                }
            });

        })
        function printThis(goodsId,obj) {

            $.ajax({
                url: '/express/getBarCode',
                type: 'POST',
                data: {"goodsId":goodsId},
                dataType: "json",
                cache: "false",
                success: function (result) {
                    if (result.result == 200) {
                        toastr.success(result.msg);
                        $(obj).remove();
                    }
                },
                error: function (err) {
                    toastr.error("网络错误...");
                }
            })

        }
        function selectAll() {
           alert( $("#selectAll").prop("checked"));
           if($("#selectAll").prop("checked")){
               $(":checkbox").prop("checked",true);
           }else{
               $(":checkbox").prop("checked",false);
           }
        }
        var i= 0;
        function ensure(expressId,orderId,obj) {
            /*<![CDATA[*/
            $("#orderforSubmit").append("<input type='hidden' name='orders["+i+"].expressId' value='"+expressId+"'/><input type='hidden' name='orders["+i+"].orderId' value='"+orderId+"'/><input type='hidden' name='orders["+i+"].status' value='500'/>")
            i++;
            $(obj).parent().prev().text("");
            $(obj).parent().prev().append("<span class='label label-success'>已收货</span>");
            $(obj).parent().empty();
            /*]]>*/
        }
        function disReceive(expressId,orderId,obj) {
            /*<![CDATA[*/
            $("#orderforSubmit").append("<input type='hidden' name='orders["+i+"].expressId' value='"+expressId+"'/><input type='hidden' name='orders["+i+"].orderId' value='"+orderId+"'/><input type='hidden' name='orders["+i+"].status' value='300'/>")
            i++;
            $(obj).parent().prev().text("");
            $(obj).parent().prev().append("<span class='label label-danger'>未收货</span>");
            $(obj).parent().empty();
            /*]]>*/
        }
       /* function verify() {
            if($("#theadTable2").find("button").length!=0){
                return toastr.error("请操作每一条订单记录！");
            }else {
                $("#btn-verify").attr("disabled",true);
                $.ajax({
                    url: '/express/confirmAll',
                    type: 'POST',
                    data: $("#submitForm").serialize(),
                    dataType: "json",
                    cache: "false",
                    success: function (result) {
                        if (result.result == 200) {
                            $("#btn-verify").attr("disabled",false);
                            $("#verifyModal").modal("hide");
                            toastr.success(result.msg);
                            $("#orderforSubmit > input").remove();
                            $("#theadTable2 tr:gt(0)").remove();
                            i=0;
                            //tables.fnDraw();
                            window.location.reload();
                        } else {
                            toastr.error(result.msg);
                        }
                    },
                    error: function (err) {
                        toastr.error("网络错误...");
                    }
                });
            }
        }*/
        function clearAllData() {
            $("#orderforSubmit > input").remove();
            $("#theadTable2 tr:gt(0)").remove();
            i=0;
        }


    </script>


</div>
</body>
</html>
